package com.syyo.gulimall.cart.controller;

import com.syyo.common.constant.AuthConstant;
import com.syyo.gulimall.cart.interceptor.CartInterceptor;
import com.syyo.gulimall.cart.service.CartService;
import com.syyo.gulimall.cart.vo.Cart;
import com.syyo.gulimall.cart.vo.CartItem;
import com.syyo.gulimall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @ author: wang
 * @ date: 2021/4/20 16:06
 * @ Description:
 */
@Controller
public class CartController {

    @Autowired
    CartService cartService;

    /**
     * 购物车页面
     *
     * 浏览器有一个cookie user-key：标识用户身份，一个月过期
     * 如果第一次使用购物车，都会生成一个user-key
     * 浏览器每次访问都会带上这个user-key
     *
     * 登录：session里有
     * 没有登录：按照cookie的user-key来做
     * 第一次：自动创建一个临时用户
     *
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage( Model model) throws ExecutionException, InterruptedException {

        // 通过当前线程，快速得到用户信息
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        System.out.println(userInfoTo);
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";

    }

    /**
     * 将商品添加到购物车
     *
     * redirectAttributes 重定向携带数据的对象
     *      addAttribute() 将数据放在url后面
     *      addFlashAttribute() 将数据存放在session里，可以在页面取出，只能取一次
     *
     */
    @GetMapping("/addToCart")
    public String addCart(@RequestParam("skuId")Long skuId,
                          @RequestParam("num")Integer num,
                          RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

        cartService.add(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";

    }

    /**
     * 重定向购物车列表
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId")Long skuId, Model model) {
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItem);
        return "success";

    }

    /**
     * 选中购物车
     * @param skuId
     * @param
     * @return
     */
    @GetMapping("/itemCheck")
    public String itemCheck(@RequestParam("skuId")Long skuId,
                            @RequestParam("check")Integer check) {
        cartService.itemCheck(skuId,check);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 选中购物车
     * @param skuId
     * @param
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId")Long skuId,
                            @RequestParam("num")Integer num) {
        cartService.countItem(skuId,num);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 选中购物车
     * @param skuId
     * @param
     * @return
     */
    @GetMapping("/delItem")
    public String delItem(@RequestParam("skuId")Long skuId) {
        cartService.delItem(skuId);

        return "redirect:http://cart.gulimall.com/cart.html";
    }


    /**
     * 获取当前用户的所有购物项
     */
    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItem> getCurrentUserCartItems() {
        List<CartItem> currentUserCartItems = cartService.getCurrentUserCartItems();
        return currentUserCartItems;

    }
}
